using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
using System.Security.Principal;
using System.Collections;
using System.Deployment.Application;
namespace MES
{    
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.txtUsername.Text.Trim() != string.Empty && txtPassword.Text.Trim() != string.Empty)
            {
                if (Authenticate(txtUsername.Text.Trim(),txtPassword.Text.Trim(),"DYNAPACK"))
	            {
                    //this.Close();
                    //new MainForm("ADMIN").ShowDialog();

                    if (IsLoginValid(MES.Utility.ADAccess.DomainGroups(txtUsername.Text.Trim())) == true)
                    {
                        new MainForm("ADMIN").ShowDialog();
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("You are not allowed to access the MES System." + System.Environment.NewLine + "Please contact the MES Team for support", "Application Notice");
                    }
                    
	            }
                else
                {
                    MessageBox.Show("Username or Password is error!", "Error Message", MessageBoxButtons.OK);
                }
            }           
        }
        private bool Authenticate(string userName,string password, string domain)
        {
            bool authentic = false;
            try
            {
                DirectoryEntry entry = new DirectoryEntry(@"LDAP://" + domain, userName, password);
                object nativeObject = entry.NativeObject;
                authentic = true;
            }
            catch (DirectoryServicesCOMException)
            {
                authentic = false;
            }
            catch (System.Runtime.InteropServices.COMException)
            {
                authentic = false;
            }
            return authentic;
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
           txtUsername.Text = GetCurrentUser();           
           lblVersion.Text = ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
        }

        private string GetCurrentUser()
        {
            string name = WindowsIdentity.GetCurrent().Name.ToString();
            return name.Substring(name.IndexOf(@"\") + 1);
        }
        private bool IsLoginValid(ArrayList ar)
        {
            bool res = false;
            IEnumerator ie = ar.GetEnumerator();
            while (ie.MoveNext())
            {
                if (ie.Current.ToString().Contains("IMD"))
                {
                    res = true;
                    break;
                }
            }
            return res;           
        }
    }
}